package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.JsonResult;
import com.qf.entity.SysNews;
import com.qf.service.SysNewsService;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("news")
public class SysNewsController {

    @Autowired
    private SysNewsService sysNewsService;


    //查询公告列表 & 模糊查询
    @RequestMapping("getAll")
    public JsonResult getAll(
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "3")Integer pageSize,
        String title
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<SysNews> newsList = sysNewsService.loadAllNews(title);
        System.out.println(newsList);
        PageInfo<SysNews> pageInfo = new PageInfo<SysNews>(newsList);
        JsonResult result = JsonResult.success();
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

    @RequestMapping("addNews")
    public String addNews(SysNews sysNews){
        System.out.println(sysNews);
        sysNewsService.addNews(sysNews);
        return "success";
    }

    @RequestMapping("delNews")
    public String delNews(Integer id){
        sysNewsService.delNews(id);
        return "success";
    }

    @RequestMapping("deleteBatchNews")
    public String deleteBatchNews(Integer[] ids){
        sysNewsService.deleteBatchNews(ids);
        return "success";
    }

    @RequestMapping("updateNews")
    public void updateNews(SysNews sysNews){
        System.out.println(sysNews);
        sysNewsService.updateNews(sysNews);
    }



}
